package com.youthfilmic.yuejian.dao;

import com.youthfilmic.yuejian.domain.Subject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.From;


/**
 * Created by youweixi on 15/10/7.
 */
public interface SubjectDao extends CrudRepository<Subject , Long> {

    Page<Subject> findByStatus(int status , Pageable pageable);

    Page<Subject> findByStatusAndUserId(int status , Long userId , Pageable pageable);

    Subject findByStatusAndId(int status , Long id);

    Page<Subject> findByUserId(Long userId , Pageable pageable);

    /**
     * 关联表查询制定城市的subject
     * */
    @Query("select subject from Subject subject,User user where subject.userId = user.id and user.city = ?1 and subject.status = ?2")
    public Page<Subject> findByCityAndStatus(int city,int status , Pageable pageable);

    public Page<Subject> findByTypeAndStatus(int type,int status , Pageable pageable);

    public Page<Subject> findAll(Pageable pageable);

    @Modifying
    @Transactional
    @Query("update Subject subject set subject.metCount = subject.metCount + ?2 where subject.id = ?1")
    void incMetCount( Long id , int inc);

    @Modifying
    @Transactional
    @Query("update Subject subject set subject.commentCount = subject.commentCount + ?2 , subject.evaluation = ?3 where subject.id = ?1")
    void incCommentCountAndEvaluation( Long id , int inc , int evaluation);
}
